{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6156e679",
   "metadata": {},
   "source": [
    "# Adjusted Q-Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d18d55e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735b42df",
   "metadata": {},
   "source": [
    "#### Define Function for Finding q-values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9fa0641d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import stats\n",
    "def fdr(p_vals):\n",
    "    from scipy.stats import rankdata\n",
    "    ranked_p_values = rankdata(p_vals)\n",
    "    fdr = p_vals * len(p_vals) / ranked_p_values\n",
    "    fdr[fdr > 1] = 1\n",
    "    return fdr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55ba074f",
   "metadata": {},
   "source": [
    "#### Calculate q-values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bbfeba34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number</th>\n",
       "      <th>p-values</th>\n",
       "      <th>q</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>311</td>\n",
       "      <td>8.340060e-06</td>\n",
       "      <td>3.252623e-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>321</td>\n",
       "      <td>3.603360e-15</td>\n",
       "      <td>4.684368e-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>322</td>\n",
       "      <td>5.999110e-52</td>\n",
       "      <td>2.339653e-50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>331</td>\n",
       "      <td>3.999014e-02</td>\n",
       "      <td>7.089161e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>332</td>\n",
       "      <td>2.461280e-08</td>\n",
       "      <td>1.919798e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>341</td>\n",
       "      <td>1.562580e-06</td>\n",
       "      <td>6.771180e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>342</td>\n",
       "      <td>5.130400e-04</td>\n",
       "      <td>1.176974e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>411</td>\n",
       "      <td>9.542950e-06</td>\n",
       "      <td>3.383410e-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>421</td>\n",
       "      <td>2.942984e-01</td>\n",
       "      <td>3.825880e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>422</td>\n",
       "      <td>4.578719e-03</td>\n",
       "      <td>8.928502e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>431</td>\n",
       "      <td>9.645630e-07</td>\n",
       "      <td>5.373994e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>441</td>\n",
       "      <td>2.085210e-04</td>\n",
       "      <td>5.082699e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>442</td>\n",
       "      <td>8.415736e-01</td>\n",
       "      <td>8.637203e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>451</td>\n",
       "      <td>1.773650e-05</td>\n",
       "      <td>5.320950e-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>452</td>\n",
       "      <td>6.915960e-09</td>\n",
       "      <td>6.743061e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>611</td>\n",
       "      <td>2.285792e-01</td>\n",
       "      <td>3.183782e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>612</td>\n",
       "      <td>8.489451e-02</td>\n",
       "      <td>1.324354e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>621</td>\n",
       "      <td>1.246600e-04</td>\n",
       "      <td>3.241160e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>622</td>\n",
       "      <td>8.269569e-01</td>\n",
       "      <td>8.716573e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>631</td>\n",
       "      <td>4.241880e-01</td>\n",
       "      <td>5.336559e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>632</td>\n",
       "      <td>5.187788e-02</td>\n",
       "      <td>8.796683e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>641</td>\n",
       "      <td>7.415736e-01</td>\n",
       "      <td>8.033714e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>642</td>\n",
       "      <td>2.084595e-02</td>\n",
       "      <td>3.871391e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>701</td>\n",
       "      <td>7.156950e-07</td>\n",
       "      <td>4.652017e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>702</td>\n",
       "      <td>5.825984e-01</td>\n",
       "      <td>6.885254e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>703</td>\n",
       "      <td>2.366490e-24</td>\n",
       "      <td>4.614656e-23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>704</td>\n",
       "      <td>1.621230e-05</td>\n",
       "      <td>5.268998e-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>705</td>\n",
       "      <td>1.167160e-06</td>\n",
       "      <td>5.689905e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>706</td>\n",
       "      <td>1.082423e-03</td>\n",
       "      <td>2.345250e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>707</td>\n",
       "      <td>5.808660e-02</td>\n",
       "      <td>9.439073e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>708</td>\n",
       "      <td>1.630839e-03</td>\n",
       "      <td>3.347512e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>709</td>\n",
       "      <td>8.493968e-01</td>\n",
       "      <td>8.493968e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>710</td>\n",
       "      <td>6.033251e-01</td>\n",
       "      <td>6.920494e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>711</td>\n",
       "      <td>2.156363e-01</td>\n",
       "      <td>3.114746e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>712</td>\n",
       "      <td>6.601532e-01</td>\n",
       "      <td>7.355992e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>713</td>\n",
       "      <td>1.242792e-01</td>\n",
       "      <td>1.864188e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>714</td>\n",
       "      <td>2.198400e-05</td>\n",
       "      <td>6.124114e-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>715</td>\n",
       "      <td>2.466779e-01</td>\n",
       "      <td>3.317392e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>716</td>\n",
       "      <td>5.225057e-01</td>\n",
       "      <td>6.368038e-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Number      p-values             q\n",
       "0      311  8.340060e-06  3.252623e-05\n",
       "1      321  3.603360e-15  4.684368e-14\n",
       "2      322  5.999110e-52  2.339653e-50\n",
       "3      331  3.999014e-02  7.089161e-02\n",
       "4      332  2.461280e-08  1.919798e-07\n",
       "5      341  1.562580e-06  6.771180e-06\n",
       "6      342  5.130400e-04  1.176974e-03\n",
       "7      411  9.542950e-06  3.383410e-05\n",
       "8      421  2.942984e-01  3.825880e-01\n",
       "9      422  4.578719e-03  8.928502e-03\n",
       "10     431  9.645630e-07  5.373994e-06\n",
       "11     441  2.085210e-04  5.082699e-04\n",
       "12     442  8.415736e-01  8.637203e-01\n",
       "13     451  1.773650e-05  5.320950e-05\n",
       "14     452  6.915960e-09  6.743061e-08\n",
       "15     611  2.285792e-01  3.183782e-01\n",
       "16     612  8.489451e-02  1.324354e-01\n",
       "17     621  1.246600e-04  3.241160e-04\n",
       "18     622  8.269569e-01  8.716573e-01\n",
       "19     631  4.241880e-01  5.336559e-01\n",
       "20     632  5.187788e-02  8.796683e-02\n",
       "21     641  7.415736e-01  8.033714e-01\n",
       "22     642  2.084595e-02  3.871391e-02\n",
       "23     701  7.156950e-07  4.652017e-06\n",
       "24     702  5.825984e-01  6.885254e-01\n",
       "25     703  2.366490e-24  4.614656e-23\n",
       "26     704  1.621230e-05  5.268998e-05\n",
       "27     705  1.167160e-06  5.689905e-06\n",
       "28     706  1.082423e-03  2.345250e-03\n",
       "29     707  5.808660e-02  9.439073e-02\n",
       "30     708  1.630839e-03  3.347512e-03\n",
       "31     709  8.493968e-01  8.493968e-01\n",
       "32     710  6.033251e-01  6.920494e-01\n",
       "33     711  2.156363e-01  3.114746e-01\n",
       "34     712  6.601532e-01  7.355992e-01\n",
       "35     713  1.242792e-01  1.864188e-01\n",
       "36     714  2.198400e-05  6.124114e-05\n",
       "37     715  2.466779e-01  3.317392e-01\n",
       "38     716  5.225057e-01  6.368038e-01"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qvalues = pd.read_csv('C:\\\\Users\\\\galle\\\\Dropbox\\\\Research\\\\Working Papers\\\\Inequality and Fairness Views\\\\Manuscript\\\\Solo 2 Submissions\\\\JRI Submission\\\\Final Submission\\\\Gallenstein_JRI_InequalityImpedesRiskManegement_Code&Data\\\\InequalityImpedeRiskManagement_p-values_uncorrected.csv')\n",
    "ps = qvalues['p-values'].values\n",
    "q=fdr(ps)\n",
    "qvalues['q'] = q\n",
    "qvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30f951d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
